package final

import (
	"context"
)

type Pool struct {
	pool chan *Worker
}

func NewPool(workers ...*Worker) *Pool {
	pool := make(chan *Worker, len(workers))
	for _, worker := range workers {
		pool <- worker
	}
	return &Pool{
		pool: pool,
	}
}

func (p *Pool) Get(ctx context.Context) *Worker {
	return <-p.pool
}

func (p *Pool) Release(ctx context.Context, worker *Worker) {
	p.pool <- worker
}

func (p *Pool) Close() {
	for worker := range p.pool {
		worker.Stop()
	}
	close(p.pool)
}
